<template>
    <div class="page-profile">
        <UserInfo />
        <ProfileInfo :messages="messages" />
        <Promotion v-if="false" />
        <Collect />
    </div>
</template>

<script>
/* eslint-disable vue/component-definition-name-casing */
import UserInfo from "./components/userInfo/index";
import ProfileInfo from "./components/profileInfo/index";
import Promotion from "./components/promotion/index";
import Collect from "./components/collect/index";
export default {
    name: "Profile",
    components: {
        UserInfo,
        ProfileInfo,
        Promotion,
        Collect
    },

    data() {
        return {
            userInfo: {},
            messages: []
        };
    },

    created() {
        this.getMessages();
        this.$bus.$on("updateUserInfo", () => {
            this.getUserInfo();
        });

        // test
        console.log("query", this.$route.query);
    },

    methods: {
        getUserInfo() {
            let loginState = this.$login.hasLoginState();
            if (loginState) {
                this.userInfo = loginState.user;
                this.$store.commit("updateUserInfo", {
                    isLogin: true,
                    ...loginState.user
                });
            } else {
                this.$store.commit("updateUserInfo", {
                    isLogin: false
                });
            }
            console.log(loginState.user);
        },

        async getMessages() {
            const col = this.$cloud.database().collection("user_message");
            const res = await col
                .where({
                    uid: this.$store.state.userInfo.uid
                })
                .get();

            if (res.data.length === 0) {
                await col.add({
                    uid: this.$store.state.userInfo.uid,
                    name: "系统通知",
                    title: "欢迎使用",
                    desc: "如有问题可在线反馈",
                    path:
                        "/article/detail?docId=b00064a76092328f14d0f85b79e8a831",
                    button: "立即查看",
                    isRead: false
                });
            }

            this.messages = res.data;
            console.log("消息列表", res.data);
        }
    }
};
</script>


<style scoped>
.page-profile {
    background: #f5f5f5;
    width: 100vw;
    min-height: 100vh;
    box-sizing: border-box;
    padding: 0 0 130px 0;
}
</style>